/*
 * DBConnection.java
 *
 * Created on 22 November 2007, 13:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.mitac.mcd.mobiletracking.managementclient.impl;


import java.beans.PropertyVetoException;
import java.sql.*;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mitac.mcd.mobiletracking.servlets.initParameters;


public class DBConnection {

	private static ComboPooledDataSource ds;
    
    public static synchronized Connection getConnection() throws SQLException, PropertyVetoException{
    	java.sql.Connection db = null;
    	
    	if(ds==null) initConnection();
    	db = ds.getConnection();
    	
    	return db;
    }
    
    public static void initConnection() throws PropertyVetoException, SQLException{
    	//设置DataSource
    	ds = new ComboPooledDataSource();
    	
    	String username = initParameters.ConfigProperties.get("DB_USERNAME").toString();
        String password = initParameters.ConfigProperties.get("DB_PASSWORD").toString();
    	String ip = initParameters.ConfigProperties.get("DB_IP").toString();
        String port = initParameters.ConfigProperties.get("DB_PORT").toString();
        String dbname = initParameters.ConfigProperties.get("DB_NAME").toString();
    	String type = initParameters.ConfigProperties.get("DB_TYPE").toString();
    	String url = null;String driverClassName = null;
    	if (type.equalsIgnoreCase("mysql")){
            driverClassName = "com.mysql.jdbc.Driver";
            url = "jdbc:mysql://"+ip+":"+port+"/"+dbname;
    	}else if (type.equalsIgnoreCase("db2")){
            driverClassName = "com.ibm.db2.jcc.DB2Driver";
            url = "jdbc:db2://"+ip+":"+port+"/"+dbname;
    	}
    	ds.setDriverClass(driverClassName);
    	ds.setJdbcUrl(url);
    	ds.setUser(username);
    	ds.setPassword(password);
    	
    	ds.setAcquireIncrement(Integer.parseInt(initParameters.jdbcProperties.get("acquireIncrement").toString()));
    	ds.setAcquireRetryAttempts(Integer.parseInt(initParameters.jdbcProperties.get("acquireRetryAttempts").toString()));
    	ds.setAcquireRetryDelay(Integer.parseInt(initParameters.jdbcProperties.get("acquireRetryDelay").toString()));
    	ds.setAutoCommitOnClose(Boolean.parseBoolean(initParameters.jdbcProperties.get("autoCommitOnClose").toString()));
    	ds.setAutomaticTestTable(initParameters.jdbcProperties.get("automaticTestTable").toString());
    	ds.setBreakAfterAcquireFailure(Boolean.parseBoolean(initParameters.jdbcProperties.get("breakAfterAcquireFailure").toString()));
    	ds.setCheckoutTimeout(Integer.parseInt(initParameters.jdbcProperties.get("checkoutTimeout").toString()));
    	ds.setIdleConnectionTestPeriod(Integer.parseInt(initParameters.jdbcProperties.get("idleConnectionTestPeriod").toString()));
    	ds.setInitialPoolSize(Integer.parseInt(initParameters.jdbcProperties.get("initialPoolSize").toString()));
    	ds.setMaxIdleTime(Integer.parseInt(initParameters.jdbcProperties.get("maxIdleTime").toString()));
    	ds.setMaxPoolSize(Integer.parseInt(initParameters.jdbcProperties.get("maxPoolSize").toString()));
    	ds.setMinPoolSize(Integer.parseInt(initParameters.jdbcProperties.get("minPoolSize").toString()));
    	ds.setMaxStatements(Integer.parseInt(initParameters.jdbcProperties.get("maxStatements").toString()));
    	ds.setMaxStatementsPerConnection(Integer.parseInt(initParameters.jdbcProperties.get("maxStatementsPerConnection").toString()));
    	ds.setNumHelperThreads(Integer.parseInt(initParameters.jdbcProperties.get("numHelperThreads").toString()));
    	ds.setPreferredTestQuery(initParameters.jdbcProperties.get("preferredTestQuery").toString());
    	ds.setPropertyCycle(Integer.parseInt(initParameters.jdbcProperties.get("propertyCycle").toString()));
    	ds.setTestConnectionOnCheckout(Boolean.parseBoolean(initParameters.jdbcProperties.get("testConnectionOnCheckout").toString()));
    	ds.setTestConnectionOnCheckin(Boolean.parseBoolean(initParameters.jdbcProperties.get("testConnectionOnCheckin").toString()));
    	ds.getConnection();//test connect db,if fail then throw exception
    }
    
    
    public void close() {
    	
    }
    
}
